package com.haojiang.mh.remote.adapter


import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import cn.com.heaton.blelibrary.ble.utils.ByteUtils
import com.haojiang.mh.AppContext
import com.haojiang.mh.R
import com.haojiang.mh.remote.utils.BleUtil
import com.haojiang.mh.remote.utils.BleUtil.Companion.lastConnect
import com.haojiang.mh.remote.utils.CmdKey
import com.haojiang.mh.remote.utils.SpUtil
import com.haojiang.mh.remote.widget.AlarmItemView
import kotlinx.android.synthetic.main.item_alarm.view.*


class AlarmMsgFragAdapter : RecyclerView.Adapter<AlarmMsgFragAdapter.ViewHolder>() {

    private val SECTION_ALARM_MSG: String = "section_alarm_msg"
    private val context = AppContext.instance()
    private val viewTypeList: MutableList<Int> = mutableListOf()    //视图类型列表--功能代码
    private val viewList: MutableList<View> = mutableListOf()   //视图列表
    private val selectCodeList = ArrayList<String>()    //选择代码列表

    @Synchronized
    fun setData(alarmList: ArrayList<String>) {
        if (lastConnect.bleDeviceList.size > 0) {
            val codeLIst = SpUtil.getListData(context, SECTION_ALARM_MSG, lastConnect.bleDeviceList.get(0).bleAddress, String::class.java)
            selectCodeList.clear()
            selectCodeList.addAll(codeLIst)

            viewList.clear()
            viewTypeList.clear()
            alarmList.forEach {
                if (it.equals(CmdKey.MSG_HEAD_INTENSITY_INC) ||
                    it.equals(CmdKey.MSG_FOOT_INTENSITY_INC)
                ) {
                    viewTypeList.add(ByteUtils.hexToInt(it))
                }
            }
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return viewTypeList.size
    }

    fun getMsgList(): ArrayList<String> {
        val msgList = ArrayList<String>()
        viewList.forEach {
            if (it.isSelected) {
                msgList.add(it.rmText.code!!)
            }
        }
        if (BleUtil.isDevConncted()) {
            if (msgList.size > 0) {
                lastConnect.bleDeviceList.forEach {
                    SpUtil.putListData(context, SECTION_ALARM_MSG, it.bleAddress, msgList)
                }
            } else {
                lastConnect.bleDeviceList.forEach {
                    SpUtil.deleteSectionKey(context, SECTION_ALARM_MSG, it.bleAddress)
                }
            }
        }
        return msgList
    }

    /**
     * 设置界面
     */
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        if (!viewList.contains(holder.itemView)) {
            viewList.add(holder.itemView)
            for (i in 0 until selectCodeList.size) {
                if (selectCodeList[i].equals(holder.itemView.rmText.code, ignoreCase = true)) {
                    holder.itemView.isSelected = true
//                    holder.itemView.ivSelector.setImageResource(R.drawable.ic_select)
                    holder.itemView.switchCheck.isChecked = true
                    break
                } else {
                    holder.itemView.isSelected = false
//                    holder.itemView.ivSelector.setImageResource(R.drawable.ic_unselect)
                    holder.itemView.switchCheck.isChecked = false
                }
            }

            holder.itemView.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    v?.run {
                        viewList.forEach {
                            if (it.equals(v)) {
                                it.isSelected = !it.isSelected
                                if (it.isSelected) {
                                    v.switchCheck.isChecked = true
                                } else {
                                    it.switchCheck.isChecked = false
                                }
                            }
                        }
                    }

//                    viewList.forEach {
//                        if (it.equals(v!!)) {
//                            it.isSelected = !it.isSelected
//                            if (it.isSelected) {
//                                v.switchCheck.isChecked = true
//                            } else {
//                                it.switchCheck.isChecked = false
//                            }
//                        } else {
//                            it.isSelected = false
//                            it.switchCheck.isChecked = false
//                        }
//                    }
                }
            })

//            holder.itemView.setOnClickListener(object : View.OnClickListener {
//                override fun onClick(v: View?) {
//                    Util.playSound()
//                    v!!.isSelected = !v.isSelected
//                    if (v.isSelected) {
////                        v.ivSelector.setImageResource(R.drawable.ic_select)
//                        v.switchCheck.isChecked = true
//                    } else {
////                        v.ivSelector.setImageResource(R.drawable.ic_unselect)
//                        v.switchCheck.isChecked = false
//                    }
//                }
//            })
        }
    }

    override fun getItemViewType(position: Int): Int {
        return viewTypeList.get(position)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val viewHolder = ViewHolder(AlarmItemView(parent.context))
        viewHolder.itemView.rmText.code = ByteUtils.intToHex(viewType)
        when (viewType) {
            ByteUtils.hexToInt(CmdKey.MSG_HEAD_INTENSITY_INC) -> {
                viewHolder.itemView.rmText.setText(context.getString(R.string.f_msg_head))
            }

            ByteUtils.hexToInt(CmdKey.MSG_FOOT_INTENSITY_INC) -> {
                viewHolder.itemView.rmText.setText(context.getString(R.string.f_msg_foot))
            }
        }
        return viewHolder
    }


    //设置选中功能
//    fun setSelectData(codeList: ArrayList<String>) {
//        this.selectCodeList.clear()
//        this.selectCodeList.addAll(codeList)
//    }


    /**
     * adapter的ViewHolder类
     */
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {}

}
